package com.panopset.app.swiftwheel;

import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.StringWriter;
import java.util.Collections;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

import javax.swing.JPanel;

import com.panopset.flywheel.Flywheel;
import com.panopset.sf.FontManager;
import com.panopset.sf.Windoc;

/**
 * Flywheel variable prompter.
 *
 * @author Karl Dinwiddie
 *
 */
public final class FlywheelPrompter {

    /**
     * OSGW panel.
     */
    private JPanel p;

    /**
     *
     * @param g
     *            Graphics.
     * @param dim
     *            Dimension.
     */
    synchronized void paintVars(final Graphics2D g, final Dimension dim) {
        g.setColor(Color.CYAN);
        g.fillRect(0, 0, dim.width, dim.height);
        g.setColor(Color.BLUE);
        if (fw == null) {
            return;
        }
        int y = FontManager.getInstance().getMonospace().getSize();
        int spacing = y + (int) (y / 3);
        y = spacing;
        for (Entry<String, String> e : fw.getAllValues().entrySet()) {
            if (e.getKey().indexOf("com.panopset") == 0) {
                continue;
            }
            g.setColor(Color.DARK_GRAY);
            g.drawString(e.getKey(), 3, y);
            y = y + spacing;
            g.setColor(Color.BLUE);
            g.drawString(e.getValue(), 3, y);
            sels.put(y, e.getKey());
            y = y + spacing;
        }
//        g.setColor(Color.RED);
//        g.drawString(getEventManager().getLastClickText(), 3, y);

    }

    Map<Integer, String> sels = Collections.synchronizedSortedMap(new TreeMap<Integer, String>());

    synchronized String getClickedText() {
        int y = getEventManager().clickPointY;
        for (Entry<Integer, String> e : sels.entrySet()) {
            if (y < e.getKey()) {
                return "${" + e.getValue() + "}";
            }
        }
        return "";
    }

    EventManager eventManager;
    EventManager getEventManager() {
        if (eventManager == null) {
            eventManager = new EventManager();
        }
        return eventManager;
    }
    class EventManager {
        int clickPointX = 0;
        int clickPointY = 0;

        public String getLastClickText() {
            StringWriter sw = new StringWriter();
            sw.append("" + clickPointX);
            sw.append(",");
            sw.append("" + clickPointY);
            return sw.toString();
        }

        public void reportMouseClick(int x, int y) {
            clickPointX = x;
            clickPointY = y;
        }

    }
    /**
     * @return OSGW panel.
     */
    public Component getPanel() {
        if (p == null) {
            FlywheelPrompterDisplay fpd = new FlywheelPrompterDisplay(this);
            p = fpd.getPanel();
            p.setCursor(new Cursor(Cursor.HAND_CURSOR));
            p.addMouseListener(new MouseAdapter() {

                @Override
                public void mouseClicked(MouseEvent e) {
                    getEventManager().reportMouseClick(e.getX(), e.getY());
                    if (til != null) {
                        til.inject(getClickedText());
                    }
                }


            });
        }
        return p;
    }

    private Flywheel fw;
    public void setFlywheel(final Flywheel flywheel) {
        fw = flywheel;
    }

    private static boolean firstTime = true;

    private final Windoc w; //TODO not used?
    public FlywheelPrompter(Windoc windoc) {
        w = windoc;
        if (firstTime) {
            firstTime = false;
        } else {
            throw new RuntimeException();
        }
    }

    TextInjectListener til;

    public void setTextInjectListener(TextInjectListener textInjectListener) {
        til = textInjectListener;
    }
}
